package com.laijiale.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laijiale.dto.*;
import com.laijiale.entities.Room;
import com.laijiale.mapper.SchoolMapper;
import com.laijiale.Service.SchoolService;
import com.laijiale.entities.School;
import com.laijiale.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 12:02
 */
@Service
@CacheConfig(cacheNames = "school")
public class SchoolServiceImpl implements SchoolService {
    @Autowired
    private SchoolMapper schoolMapper;
    @CacheEvict(allEntries = true)
    @Override
    public int insert(SchoolInsertDTO schoolInsertDTO) {
        School school = BeanUtil.copyProperties(schoolInsertDTO, School.class);

        school.setId(null);
        school.setDeleted(0);
        school.setCreated(LocalDateTime.now());
        school.setUpdated(LocalDateTime.now());
        int result = schoolMapper.insert(school);
        if (result <= 0) {
            throw new RuntimeException("DB添加失败");

        }

        return result;
    }

    @Cacheable(key = "#id", condition = "#id !=null", unless = " #result==null")
    @Override
    public School selectById(Long id) {
        School result = schoolMapper.selectById(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException("ID不存在");
        }
        return result;
    }
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public int updateById(SchoolUpdateDTO schoolUpdateDTO) {
        School school = BeanUtil.copyProperties(schoolUpdateDTO, School.class);
        school.setCreated(null);
        school.setUpdated(LocalDateTime.now());
        int result = schoolMapper.updateById(school);
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        //todo 查询记录是否被引用
        int result = schoolMapper.logicDeleteById(id);
        if (result <= 0) {
            throw new ServiceException("DB逻辑删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        //todo 查询记录是否被引用
        int result = schoolMapper.logicDeleteByIds(ids);
        if (result <= 0) {
            throw new ServiceException("DB逻辑批删失败");

        }
        return result;
    }
@Cacheable(key = "#root.methodName",unless = "#result==null")
    @Override
    public List<School> list() {
        return schoolMapper.list();
    }
@Cacheable(key = "#root.methodName + ':' + " +
"#pageDTO.getPageNum()+':' + " +
"#pageDTO.getPageSize()",unless = "#result == null ")
    @Override
    public PageInfo<School> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());

        List<School> list = schoolMapper.list();
        PageInfo<School> pageInfo = new PageInfo(list);

        return pageInfo;
    }



        @Cacheable(key = "#root.methodName + ':' + " +
                "#dto.getTitle() + ':' + " +
                "#dto.getPageNum() + ':' +" +
                "#dto.getPageSize()",unless = "#result==null")
        @Override
    public PageInfo<School> pageLikeTitle(SchoolPageDTO dto) {
            PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
            return new PageInfo<>(schoolMapper.listLikeTitle(dto.getTitle()));
        }
    }

